# Contributor: wenerme <wenermail@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Matt Smith <mcs@darkregion.net>
# Contributor: Olivier Mauras <olivier@mauras.ch>
# Contributor: Daniel Isaksen <d@duniel.no>
# Contributor: Fabian Zoske <fabian@zoske.it>
# Maintainer: Peter van Dijk <peter.van.dijk@powerdns.com>
pkgname=pdns
pkgver=4.9.2
pkgrel=0
pkgdesc="PowerDNS Authoritative Server"
url="https://www.powerdns.com/"
# s390x blocked by failing testsuite
arch="all !s390x"
license="GPL-2.0-only WITH OpenSSL-Exception"
checkdepends="findutils"
makedepends="$depends_dev
	boost-dev
	curl
	curl-dev
	geoip-dev
	krb5-dev
	libmaxminddb-dev
	libpq-dev
	libsodium-dev
	lmdb-dev
	mariadb-connector-c-dev
	mariadb-dev
	openldap-dev
	openssl-dev>3
	protobuf-dev
	sqlite-dev
	unixodbc-dev
	yaml-cpp-dev
	zeromq-dev
	"
install="$pkgname.pre-install $pkgname-backend-pgsql.post-upgrade"
subpackages="$pkgname-doc $pkgname-openrc $pkgname-tools
	$pkgname-backend-bind:backend_bind
	$pkgname-backend-geoip:backend_geoip
	$pkgname-backend-ldap:backend_ldap
	$pkgname-backend-lmdb:backend_lmdb
	$pkgname-backend-lua2:backend_lua2
	$pkgname-backend-mariadb:backend_mariadb:noarch
	$pkgname-backend-mysql:backend_mysql
	$pkgname-backend-odbc:backend_odbc
	$pkgname-backend-pipe:backend_pipe
	$pkgname-backend-pgsql:backend_pgsql
	$pkgname-backend-remote:backend_remote
	$pkgname-backend-sqlite3:backend_sqlite3"
pkgusers="pdns"
pkggroups="pdns"
source="https://downloads.powerdns.com/releases/pdns-${pkgver//_/-}.tar.bz2
	$pkgname.initd
	$pkgname.conf
	"
builddir="$srcdir/$pkgname-${pkgver//_/-}"

case "$CARCH" in
loongarch64|ppc64le|riscv64)
	# these don't have luajit
	makedepends="$makedepends lua5.3-dev"
	;;
*)
	makedepends="$makedepends luajit-dev"
	;;
esac

# secfixes:
#   4.6.1-r0:
#     - CVE-2022-27227
#   4.5.1-r0:
#     - CVE-2021-36754
#   4.3.1-r0:
#     - CVE-2020-17482
#   4.1.11-r0:
#     - CVE-2019-10203
#   4.1.10-r0:
#     - CVE-2019-10162
#     - CVE-2019-10163
#   4.1.7-r0:
#     - CVE-2019-3871
#   4.1.5-r0:
#     - CVE-2018-10851
#     - CVE-2018-14626
#   4.0.5-r0:
#     - CVE-2017-15091

build() {
	./configure --prefix=/usr \
		--sysconfdir="/etc/$pkgname" \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--libdir="/usr/lib/$pkgname" \
		--with-modules="" \
		--with-dynmodules="bind geoip ldap lmdb lua2 gmysql godbc pipe gpgsql remote gsqlite3" \
		--enable-tools \
		--enable-unit-tests \
		--disable-static \
		--enable-remotebackend-zeromq \
		--with-libcrypto=/usr
	make
}

_cat_found_file() {
	local _rc="$?"
	find . -name "$1" -type f -ls -printf '%P {{{\n' -exec cat -v -n '{}' ';' -printf '}}} %P\n'
	return "$_rc"
}

check() {
	PDNS_TEST_NO_IPV6=1 make check || _cat_found_file 'test-suite.log'
}

package() {
	make DESTDIR="$pkgdir" install

	install -m755 -D "$srcdir/$pkgname.initd" \
		"$pkgdir/etc/init.d/$pkgname"
	rm "$pkgdir/etc/$pkgname/$pkgname.conf-dist"
	install -m600 -D "$srcdir/$pkgname.conf" \
		"$pkgdir/etc/$pkgname/$pkgname.conf"
	chown pdns:pdns "$pkgdir/etc/$pkgname/$pkgname.conf"

	# pdns-backend-pgsql.post-upgrade needs to be updated when this file path changes
	cp -v -p "$pkgdir/usr/share/doc/pdns/4.1.0_to_4.2.0_schema.pgsql.sql" "$pkgdir/etc/$pkgname/"
}

tools() {
	_tool_names="zone2ldap zone2sql zone2json
		dnsgram dnsreplay dnsscan dnsscope dnswasher dumresp
		pdns_notify nproxy nsec3dig saxfr stubquery ixplore
		sdig calidns dnsbulktest dnstcpbench dnspcap2protobuf"

	mkdir -p "$subpkgdir"
	cd "$subpkgdir"

	mkdir -p "$subpkgdir/usr/bin"
	for _tool in $_tool_names; do
		mv "$pkgdir/usr/bin/$_tool" "$subpkgdir/usr/bin/"
	done; unset -v _tool
}

_backend() {
	local backend=$1
	shift

	pkgdesc="$backend backend module for PowerDNS"
	depends="$pkgname $*"

	mkdir -p "$subpkgdir"
}

_mv_backend() {
	local backend=$1
	_backend "$@"

	mkdir -p "$subpkgdir/usr/lib/pdns/pdns"
	mv "$pkgdir/usr/lib/pdns/pdns/lib${backend}backend.so" \
		"$subpkgdir/usr/lib/pdns/pdns/"
}

backend_bind() { _mv_backend bind; }
backend_geoip() { _mv_backend geoip geoip; }
backend_ldap() { _mv_backend ldap; }
backend_lmdb() { _mv_backend lmdb lmdb; }
backend_lua2() { _mv_backend lua2 lua luajit; }
backend_mariadb() { _backend mariadb $pkgname-backend-mysql; }
backend_mysql() { _mv_backend gmysql mariadb-client; }
backend_odbc() { _mv_backend godbc unixodbc; }
backend_pipe() { _mv_backend pipe; }
backend_pgsql() { _mv_backend gpgsql postgresql-client; }
backend_remote() { _mv_backend remote; }
backend_sqlite3() { _mv_backend gsqlite3 sqlite; }
#backend_tinydns() { _mv_backend tinydns; }

# temporary split for 4.1.11 schema fix
backend_pgsql() {
	_mv_backend gpgsql postgresql-client

	mkdir -p "$subpkgdir/etc/$pkgname"
	mv "$pkgdir/etc/$pkgname/4.1.0_to_4.2.0_schema.pgsql.sql" "$subpkgdir/etc/$pkgname/"
}

sha512sums="
42292d5dadadf39077a5be2bb0f973292375e2d0695738bfee73d91681f9e7b0e313343dafa042ce3c7141e3132ee5756cb42eabab2188c02adb44fa07866235  pdns-4.9.2.tar.bz2
3a55547e1b6407e7d2faa6e02982ed903c2364381af1b7eeb626ae3a8b0e32558dd79bf31c982b134414e5636d4868c1f3660ac523f25d2440ed6f7b436843bf  pdns.initd
b46bda3731fe91715d4c2e5e735a331505859d30e5c47e24a50484e091b9b564ddac59c762babf2c8369b483038bfb78c350d7b877a38ed188f3189b026d989d  pdns.conf
"
